package com.mytaobao.main.weitao;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

import com.baseproject.mvp.MvpFragment;
import com.jaeger.library.StatusBarUtil;
import com.mytaobao.R;
import com.mytaobao.main.TabFragmentAdapter;

/**
 * Created by hp on 2017/9/4.
 */

public class WeiTaoFragment extends MvpFragment<WeiTaoFragment, WeiTaoPresenter>
        implements WeiTaoContract.IWeiTaoView {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    //    private int[] tabIcons = {
//            R.drawable.selector_dongtai,
//            R.drawable.selector_shangxin,
//            R.drawable.selector_zhibo,
//            R.drawable.selector_rebang
//    };
    private int[] tabIcons = {
            R.drawable.selector_dongtai,
            R.drawable.selector_dongtai,
            R.drawable.selector_dongtai,
            R.drawable.selector_dongtai
    };

    public String[] title = new String[]{"关注动态", "宝贝上新", "视频直播", "精选热榜"};
    private AppBarLayout mAppBarLayout;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_weitao;
    }

    @Override
    protected void _onCreateView(View view, ViewGroup container, Bundle savedInstanceState) {
        mTabLayout = (TabLayout) view.findViewById(R.id.id_tablayout);
        mViewPager = (ViewPager) view.findViewById(R.id.id_pager);
        mAppBarLayout = (AppBarLayout) view.findViewById(R.id.appbar);
        mAdapter = new TabFragmentAdapter(getActivity().getSupportFragmentManager(), title);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        //指定Tab的位置
        mTabLayout.getTabAt(0).setCustomView(getTabView(0));
        mTabLayout.getTabAt(1).setCustomView(getTabView(1));
        mTabLayout.getTabAt(2).setCustomView(getTabView(2));
        mTabLayout.getTabAt(3).setCustomView(getTabView(3));
    }

    @Override
    protected void initStatusBar() {
        StatusBarUtil.setColor(this.getActivity(), getResources().getColor(R.color.topBarCart));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @NonNull
    @Override
    public WeiTaoPresenter createPresenter() {
        return new WeiTaoPresenter();
    }

    public View getTabView(int position) {
        WeiTaoTabCustomView weiTaoTabCustomView = new WeiTaoTabCustomView(this.getActivity().getApplicationContext(), mAppBarLayout);
        weiTaoTabCustomView.setText(title[position]);
        weiTaoTabCustomView.setIcon(tabIcons[position]);
        return weiTaoTabCustomView;
    }
}
